package com.sc.nft.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.SaasTaskDao;
import com.sc.nft.dao.SaasTaskMeetConditionsDao;
import com.sc.nft.dao.SaasTaskMeetConditionsLocationDao;
import com.sc.nft.dao.SaasUserTaskDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.TaskParticipationRecordDTO;
import com.sc.nft.dto.TaskRewardListDTO;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.TaskStarRewardListDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.SaasUserTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户任务命中(SaasUserTask)表服务实现类
 *
 * @author 王伟成
 * @since 2024-01-11 15:10:00
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SaasUserTaskServiceImpl implements SaasUserTaskService {


    private final SaasUserTaskDao saasUserTaskDao;


    private final SaasTaskDao saasTaskDao;

    private final SaasTaskMeetConditionsDao saasTaskMeetConditionsDao;

    private final SaasTaskMeetConditionsLocationDao saasTaskMeetConditionsLocationDao;
    @Override
    public IPage<TaskRewardListVO> taskRewardList(TaskRewardListDTO taskRewardListDTO) {
        IPage<TaskRewardListVO> page = saasUserTaskDao.taskRewardList(taskRewardListDTO);
        return page;
    }
    @Override
    public List<SaasUserTaskIdGainAwardVO> getByUserTaskIdGainAward(Long id) {
        // 获取这个任务的id和他的奖励的id.去和奖励匹配.
        SaasUserTask saasUserTask = saasUserTaskDao.getById(id);
        if (BeanUtil.isEmpty(saasUserTask)) {
            throw new GlobalRunTimeException("参数错误");
        }
        return saasUserTaskDao.getByUserTaskIdGainAward(saasUserTask.getSaasTaskId(), saasUserTask.getTaskAwardId());

    }
    @Override
    public IPage<TaskParticipationRecordVO> taskParticipationRecord(TaskParticipationRecordDTO taskParticipationRecordDTO) {
        IPage<TaskParticipationRecordVO> page = saasUserTaskDao.taskParticipationRecord(taskParticipationRecordDTO);
        return page;
    }
    @Override
    public SaasTaskCheckTheProgressVO checkTheProgress(Long id) {
        // 根据id获取任务的进度
        SaasUserTask saasUserTask = saasUserTaskDao.getById(id);
        SaasTaskCheckTheProgressVO saasTaskCheckTheProgressVO = new SaasTaskCheckTheProgressVO();
        SaasTask saasTask = saasTaskDao.getById(saasUserTask.getSaasTaskId());
        SaasTaskMeetConditions saasTaskMeetConditions = saasTaskMeetConditionsDao.getByTaskId(saasTask.getId());
        saasTaskCheckTheProgressVO.setTaskRest(saasTask.getTaskRest());
        saasTaskCheckTheProgressVO.setVoluntarySum(saasTaskMeetConditions.getVoluntarySum());
        saasTaskCheckTheProgressVO.setSchedule(saasTaskMeetConditions.getSchedule());
        saasTaskCheckTheProgressVO.setUserSchedule(saasUserTask.getSchedule());
        saasTaskCheckTheProgressVO.setTaskTypeEnum(saasTaskMeetConditions.getTaskType());
        // 获取任务生效位置
        saasTaskCheckTheProgressVO.setLocationList(saasTaskMeetConditionsLocationDao.getByTaskIdLocation(id));
        return saasTaskCheckTheProgressVO;
    }
    @Override
    public IPage<TaskStarRewardListVO> taskStarRewardList(TaskStarRewardListDTO taskStarRewardListDTO) {
        IPage<TaskStarRewardListVO> page = saasUserTaskDao.taskStarRewardList(taskStarRewardListDTO);
        return page;
    }
    @Override
    public IPage<TaskParticipationRecordVO> taskStarParticipationRecord(TaskParticipationRecordDTO taskParticipationRecordDTO) {
        IPage<TaskParticipationRecordVO> page = saasUserTaskDao.taskStarParticipationRecord(taskParticipationRecordDTO);
        return page;
    }
}
